package com.samsungmobile.common.channel;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.samsungmobile.common.Logger;
import com.samsungmobile.common.db.DBAutoManager;

public class ServletManager extends ServletRun {

	protected void init() throws Exception {
		Logger.LOG_NOHUP.info("=============================");
	    Logger.LOG_NOHUP.info("Called class : "+this.getClass());
	    Logger.LOG_NOHUP.info("=============================");
		
		printRequest();

	}
	
	protected void process() throws Exception {}
	
	/**
	 * Select 결과를 String에 담아서 리턴한다.
	 * 
	 * @param query
	 */
	protected String singleValueQuery(String sQuery){
		return DBAutoManager.singleValueQuery(sQuery);	
	}

	/**
	 * Select 결과를 String에 담아서 리턴한다.
	 * 
	 * @param query
	 */
	
	protected String singleValueQuery(String sQuery, Vector vParam){
		return DBAutoManager.singleValueQuery(sQuery, vParam);	
	}
	
	/**
	 * Select 결과를 Vector에 담아서 리턴한다.
	 * 
	 * @param query
	 */
	protected Vector selectQuery(String sQuery){
		return DBAutoManager.selectQuery(sQuery);	
	}
	
	/**
	 * Select 결과를 Vector에 담아서 리턴한다.
	 * 
	 * @param sQuery
	 * @param vParam
	 */
	protected Vector selectQuery(String sQuery, Vector vParam){
		return DBAutoManager.selectQuery(sQuery, vParam);
	}

	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(String sQuery){
		return DBAutoManager.updateQuery(sQuery);
	}
	
	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(String sQuery, Vector vParam){
		return DBAutoManager.updateQuery(sQuery, vParam);
	}
	
	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(String[] sQuerys) throws SQLException{
		return DBAutoManager.updateQuery(sQuerys);
	}
	
	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(String[] sQuerys, Vector[] vParam) throws SQLException{
		return DBAutoManager.updateQuery(sQuerys, vParam);
	}

	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(StringBuffer[] sbQuerys, Vector[] vParam) throws SQLException{
		return DBAutoManager.updateQuery(sbQuerys, vParam);
	}	
	/**
	 * insert/update/delete 결과를 true/false로 리턴한다.
	 * 
	 * @param sQuery
	 */
	protected boolean updateQuery(ArrayList parameters) throws SQLException{
		return DBAutoManager.updateQuery(parameters);
	}
	
	/**
	 * Servlet실행후 지정한 path로 페이지를 이동시킨다.
	 * 
	 * @param req
	 * @param res
	 * @param path
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void forward(HttpServletRequest req, HttpServletResponse res, String path) 
	    throws ServletException, IOException{
		//RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
		System.out.println("# sooik #");
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " + path );
		RequestDispatcher dispatcher = req.getRequestDispatcher(path);
		dispatcher.forward(req, res);
		Logger.LOG_NOHUP.info("Forward to_ " + path);
		
	}
	
	/**
	 *  Servlet실행후 생성된 데이터를 Attribute에 담아서 지정한 path로 페이지를 이동시킨다.
	 * 
	 * @param req
	 * @param res
	 * @param path
	 * @param hm
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void forward(HttpServletRequest req, HttpServletResponse res, String path, HashMap hm)
	    throws ServletException, IOException{
		//RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
		System.out.println("# sooik #");
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " + path );
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RESULT/" + path );
		RequestDispatcher dispatcher = req.getRequestDispatcher(path);
		req.setAttribute("RESULT", hm);
		dispatcher.forward(req, res);
		Logger.LOG_NOHUP.info("Forward to " + path);
	}
	
	/**
	 * Properties에서 지정된 KEY의 값을 String을 리턴한다.
	 * 
	 * @param o
	 * @param column_name
	 * @return
	 */
	protected String getRs(Object o, String column_name) {
	    return ((Properties) o).getProperty(column_name.toLowerCase()).trim();
	}
	
	
	/**
	 * 사용자에 대한 필드명을 입력받아서 해당 데이터를 리턴한다.
	 * 
	 * @param field
	 * @return
	 */
	protected String getSessionValue(String field){
		// return value
		String reValue = "";
		
		// Session값을 읽어온다.
		try {
			Vector vUserInfo = (Vector) session.getAttribute("TAPS_SESSION_USERINFO");
		
			if(vUserInfo.size()>0){
				Properties p = (Properties) vUserInfo.get(0);
				reValue = p.getProperty(field.toLowerCase());
			}
			Logger.LOG_NOHUP.info(field.toUpperCase() + " : " + reValue);
		}catch(Exception ex){
			Logger.LOG_NOHUP.info("No Session Value !!!!!!!!");
		}
		return reValue;
	}
	
	/**
	 * Servlet 호출시 넘어오는 모든 Request를 화면에 뿌려준다.
	 * 
	 * @return
	 */
	private void printRequest(){
		
		Enumeration en = req.getParameterNames();
		while(en.hasMoreElements()){
			String name = en.nextElement().toString();
			String value = req.getParameter(name);
			Logger.LOG_NOHUP.info("Parameter : " + name + " ==> " + value);
		}
	}
	
    protected boolean  updatelobQuery (String[] sQuerys, ArrayList aRrayparam, String sContent) {
        return DBAutoManager.updatelobQuery( sQuerys, aRrayparam, sContent);
    } 
    
    protected String getRsclob(String sQuery, String sField) {
        return DBAutoManager.getRsclob( sQuery, sField);
    }		
}
